package com.meilai.project.core.excel;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.util.List;

/**
 * @author xchen
 * @since 2022-02-15
 */
public abstract class AbstractSheetGenerator {
	protected SXSSFWorkbook workBook;
	protected SXSSFSheet sheet;
	protected ReportSheet reportSheet;
	//记录当前处理行
	protected int currentRowIndex = -1;

	public void init(SXSSFWorkbook workBook, ReportSheet reportSheet) {
		if(null == this.workBook) {
			this.workBook = workBook;
		}
		this.reportSheet = reportSheet;
		sheet = workBook.createSheet(this.reportSheet.getSheetName());
		initSheetGenerator();
	}

	protected CellStyle createTitleCellStyle(short fontSize) {
		CellStyle cellStyle = workBook.createCellStyle();
		cellStyle.setAlignment(HorizontalAlignment.CENTER);
		// 表头样式
		Font font = workBook.createFont();
		font.setFontName("黑体");
		font.setFontHeightInPoints(fontSize);// 字体大小
		font.setBold(true);
		cellStyle.setFont(font);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		return cellStyle;
	}

	protected CellStyle createExportTimeCellStyle() {
		CellStyle cellStyle = workBook.createCellStyle();
		cellStyle.setAlignment(HorizontalAlignment.CENTER);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
		Font font = workBook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 10);// 字体大小
		cellStyle.setFont(font);
		return cellStyle;
	}

	protected CellStyle createCommonCellStyle(int fontSize, boolean bold) {
		CellStyle style = createCellFontStyle(fontSize, bold);
		style.setBorderTop(BorderStyle.THIN);
		style.setBorderBottom(BorderStyle.THIN);
		style.setBorderLeft(BorderStyle.THIN);
		style.setBorderRight(BorderStyle.THIN);
		style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
		style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
		style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
		style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
		return style;
	}

	protected CellStyle createCellFontStyle(int fontSize, boolean bold) {
		CellStyle style = workBook.createCellStyle();
		style.setAlignment(HorizontalAlignment.CENTER);// 左右居中
		style.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
		Font font = workBook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) fontSize);// 字体大小
		font.setBold(bold);// 是否加粗
		style.setFont(font);
		return style;
	}

	protected abstract void initSheetGenerator();

	public abstract void process(List data);

	public abstract void afterProcess();

}
